package org.nlp2rdf.navigator.benchmark;

import org.aksw.commons.util.collections.PrefetchIterator;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * Created by Claus Stadler
 * Date: Oct 13, 2010
 * Time: 12:58:45 AM
 */
class KFoldIterator<E>
    extends PrefetchIterator<KFoldContext<E>>
{
    private List<Sample<E>> kFolds;
    private Iterator<Sample<E>> itTestSample;
    private Random random;

    public KFoldIterator(List<Sample<E>> kFolds, Random random)
    {
        this.kFolds = kFolds;
        this.itTestSample = kFolds.iterator();
        this.random = random;
    }

    @Override
    protected Iterator<KFoldContext<E>> prefetch()
            throws Exception
    {
        if(!itTestSample.hasNext())
            return null;

        Sample<E> testSample = itTestSample.next();

        Sample<E> fold = Sample.create();

        for(Sample<E> train : kFolds) {
            if(train == testSample)
                continue;

            fold.addAll(train.getPositives(), train.getNegatives());
        }

        KFoldContext<E> context = new KFoldContext<E>(testSample, fold);

        return Collections.singleton(context).iterator();
    }
}
